<template>
	<view class="content" v-if="!is_pc">
		<view class="user-info">
			<view class="item-info">
				<text>姓名：</text>
				<view style="position: relative;">
					<input type="text" class="item-info-input" :class="{'error_input': userInfo.username_error}" v-model="userInfo.username"
					 @blur="checkInput('username')" />
					<text style="position: absolute;color: red;font-size: 12px;" v-show="userInfo.username_error">{{userInfo.username_error_msg}}</text>
				</view>
			</view>
			<view class="item-info">
				<text>手机：</text>
				<view style="position: relative;">
					<input type="text" :maxlength="11" class="item-info-input" :class="{'error_input': userInfo.phone_error}" v-model="userInfo.phone"
					 @blur="checkInput('phone')" />
					<text style="position: absolute;color: red;font-size: 12px;" v-show="userInfo.phone_error">{{userInfo.phone_error_msg}}</text>
				</view>
			</view>
		</view>
		<view style="width: 100%;display: flex;justify-content: space-around;padding: 10px 25px;box-sizing: border-box;">
			<button style="background-color: #6ad26a;color: #FFFFFF;" size="mini" @click="toPath(1)">查看报告</button>
			<button type="primary" size="mini" @click="toPath(2)">开始填写问卷</button>
		</view>
	</view>
	<view v-else>暂不支持PC端，请在移动设备上操作</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	export default {
		data() {
			return {
				userInfo: {
					username: '',
					username_error: false,
					phone: '',
					phone_error: false
				},
				is_pc: false
				
			}
		},
		computed: {
			...mapState(['BASE_URL'])
		},
		onLoad() {
			if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
				this.is_pc = false;
			} else {
				this.is_pc = true;
			}
		},
		methods: {
			toPath(type) {
				let self = this;
				if (this.checkInput('username') || this.checkInput('phone')) {
					return false;
				}
				uni.showLoading({
					title: '请稍等...',
					mask: true
				})
				// 查看报告
				uni.request({
					url: this.BASE_URL + 'WikiCareerAssessment/checkUser',
					method: 'POST',
					data: {
						'type': type,
						'username': this.userInfo.username,
						'phone': this.userInfo.phone
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
					},
					success(res) {
						uni.hideLoading();
						if (res.data.code == 200) {
							uni.setStorageSync("userInfo", {
								'username': self.userInfo.username,
								'phone': self.userInfo.phone
							})
							if (type == 2) {
								uni.reLaunch({
									url: './questionnaire'
								})
							} else if (type == 1) {
								uni.reLaunch({
									url: './analysis_report'
								})
							}

						} else {
							if (type == 2) {
								self.userInfo.phone_error_msg = res.data.msg ? res.data.msg : '程序错误，请重试';
								self.userInfo.phone_error = true;
							} else if (type == 1) {
								self.userInfo.username_error_msg = res.data.msg ? res.data.msg : '程序错误，请重试';
								self.userInfo.username_error = true;
							}

						}
					},
					fail() {
						uni.hideLoading();
						self.userInfo.phone_error_msg = '程序错误，请重试';
						self.userInfo.phone_error = true;
					}
				})

			},
			// 校验手机号码是否合法
			checkInput(type) {
				let phoneReg = /^1[3-9][0-9]{9}$/;
				if (type == 'username') {
					this.userInfo.username_error = false;
					if (!this.userInfo.username.trim().length) {
						this.userInfo.username_error_msg = "请填写姓名";
						this.userInfo.username_error = true;
					}
				}

				if (type == 'phone') {
					this.userInfo.phone_error = false;
					if (!phoneReg.test(this.userInfo.phone)) {
						this.userInfo.phone_error_msg = "请填写正确的手机号码";
						this.userInfo.phone_error = true;
					}
				}

				if (this.userInfo[type + '_error']) {
					return true;
				}
				return false;
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 10px;

		box-sizing: border-box;
		height: 100%;
	}

	.user-info {
		margin-top: 50px;
		background-color: rgba(255, 255, 255, 0.8);
		width: 100%;
	}

	.item-info {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		margin: 20px;
		font-size: 16px;
	}

	.item-info-input {
		border-bottom: 1px solid #007AFF;
		padding: 2px 10px;
	}

	.error_input {
		border-color: red;
	}
</style>
